package com.tuandai.commons.encrypt.jdbc_api;

import com.tuandai.commons.encrypt.RsaSecretEncryptor;
import com.tuandai.commons.encrypt.RsaSecretEncryptorFactory;

/**
 * Jks解密
 * Created by zhangzhenbin on 17-8-4.
 */
public class JksDigestUtils {
    private final static String ENCRYPT_PREFIX = "{cipher}";

    public static String decrypt(String mw,String JKS_LOCATION,String JKS_PASSWORD,String JKS_ALIAS,String JKS_SECRET){

        String result = mw;
        if(JKS_LOCATION != null && JKS_PASSWORD !=null && JKS_ALIAS != null & JKS_SECRET != null){
            RsaSecretEncryptor encryptor = RsaSecretEncryptorFactory.getRsaSecretEncryptor(JKS_LOCATION, JKS_PASSWORD, JKS_ALIAS, JKS_SECRET);
            if(mw.startsWith(ENCRYPT_PREFIX)){
                try {
                    result = encryptor.decrypt(mw.substring(ENCRYPT_PREFIX.length()));
                } catch (Throwable e) {
                    //
                }
            }
        }
        return result;
    }
}
